<!DOCTYPE html>
<html lang="zh-cmn-Hans" prefix="og: http://ogp.me/ns#" class="han-init">
<head>
  <meta charset="utf-8">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <link rel="icon" href="http://upeng.github.io/favicon.ico">
  <title>upeng</title>
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/lib/fancybox/jquery.fancybox-1.3.4.css">
  <!--在这里倒入jquery 方便处理部分页面的jquery-->
  <script src="https://cdn.staticfile.org/jquery/1.7/jquery.min.js" type="text/javascript" ></script>
</head>

<body>
	<header class="site-header navfixed-false">
  <div class="container">
      <h1><a href="/" title="upeng"><span class="octicon octicon-mark-github"></span> upeng</a></h1>
      <nav class="site-header-nav" role="navigation">
        
              
              <a href="/"  class=" site-header-nav-item hvr-underline-from-center" title="Home">Home</a>
        
              
              <a href="/categories/"  class=" site-header-nav-item hvr-underline-from-center" title="Categories">Categories</a>
        
              
              <a href="/bookmark/"  class=" site-header-nav-item hvr-underline-from-center" title="Bookmark">Bookmark</a>
        
              
              <a href="http://shareup.sinaapp.com"  class=" site-header-nav-item hvr-underline-from-center" title="Share">Share</a>
        
              
              <a href="/about/"  class=" site-header-nav-item hvr-underline-from-center" title="About">About</a>
        
      </nav>
  </div>
</header>

	
<section class="collection-head geopattern" data-pattern-id="laravel的blade视图模板引擎" >
    <div class="container">
        <div class="collection-title">
            <h1 class="collection-header">
                laravel的blade视图模板引擎
            </h1>
            
                <div class="collection-info">
                    <span class="meta-info">
                        <span class="octicon octicon-calendar"></span>
                        <time datetime="2016-04-28T16:18:00.000Z" itemprop="datePublished">2016-04-29</time>
                    </span>
                    
                        <span class="meta-info">
                            <span class="octicon octicon-file-directory"></span>
                            <a href='/categories/PHP/' title=''>PHP</a>
                        </span>
                    
                </div>
            
        </div>
    </div>
</section>
	<section class="container">
    <div class="columns">
        <!-- -->
        <div class="column three-fourths">
            <article class="article-content markdown-body">
                <h3 id="blade模板引擎使用"><a href="#blade模板引擎使用" class="headerlink" title="blade模板引擎使用"></a>blade模板引擎使用</h3><p>很多时候，我们的view模板有很多重复的地方，比如head、footer部分，当然可以定义一些共用的部分，置于common文件夹下，然后使用include将其引入进来。而blade有一套自己的策略</p>
<p>比如定义一个<code>common.blade.php</code>文件，如下：</p>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Laravel&lt;/title&gt;
        &lt;link href=&quot;https://fonts.googleapis.com/css?family=Lato:100&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
        &lt;style&gt;
            html, body {height: 100%;}
            body {margin: 0;width: 100%;display: table;font-weight: 100;font-family: &#39;Lato&#39;;}
            .container {text-align: center;display: table-cell;vertical-align: middle;
            }
            .content {text-align: center;display: inline-block;}
            .title {font-size: 96px;}
            .footer{color: green;font-size: 100px;}
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt; 
        &lt;div class=&quot;container&quot;&gt;
            @yield(&#39;content&#39;)
            @yield(&#39;footer&#39;)
        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre><a id="more"></a>
<ul>
<li>其中，<code>@yield(&#39;content&#39;)</code>和 <code>@yield(&#39;footer&#39;)</code>是根据不同模板的需求进行动态设置与填充的</li>
<li><code>welcome.blade.php</code>和<code>contact.blade.php</code>模板与<code>common.blade.php</code>样式相近，只是内容不同，或者后者内容更多。他们均可以继承<code>common.blade.php</code>的所有内容，需要content则可以自定义显示，需要footer可以自定义显示footer</li>
<li>示例如下</li>
</ul>
<pre><code># welcome.blade.php
@extends(&#39;common&#39;)      //继承common.blade.php
@section(&#39;content&#39;)     //开始
    &lt;div class=&quot;content&quot;&gt;
        &lt;div class=&quot;title&quot;&gt;Laravel 5 -welcome&lt;/div&gt;
    &lt;/div&gt;
@stop                   //结束


# contact.blade.php
@extends(&#39;app&#39;)
@section(&#39;content&#39;) 
    &lt;div class=&quot;content&quot;&gt;
        &lt;div class=&quot;title&quot;&gt;contact 825415473@qq.com&lt;/div&gt;
    &lt;/div&gt;
@stop
@section(&#39;footer&#39;)
    &lt;div class=&quot;footer&quot;&gt;
        Laravel 5
    &lt;/div&gt;
@stop
</code></pre><h3 id="blade中的条件判断和循环操作"><a href="#blade中的条件判断和循环操作" class="headerlink" title="blade中的条件判断和循环操作"></a>blade中的条件判断和循环操作</h3><pre><code># 条件判断和循环操作，通用方法是在&lt;?php **** ?&gt;
# blade有自己的语法
@extends(&#39;app&#39;)
@section(&#39;content&#39;)

@if($condiction == &#39;bool&#39;)
    &lt;h1&gt;{!!$name!!}&lt;/h1&gt;
@else
    &lt;h1&gt;{{$time}}&lt;/h1&gt;
@endif

&lt;h2&gt;
    @foreach($arr as $key =&gt; $value)
        {{$value}}
    @endforeach
&lt;/h2&gt;

@stop
</code></pre>
            </article>
            
                <div class="share">
                    <!--开启分享-->
<div class="share-component" data-disabled="google,twitter,facebook" data-description="blade模板引擎使用很多时候，我们的view模板有很..."></div>

<script src="/js/share.min.js"></script>

                </div>    
            
            
                
<div class="comments">
	<div class="ds-thread" data-thread-key="laravel-blade" data-title="laravel的blade视图模板引擎" data-url="http://upeng.github.io/2016/04/29/laravel-blade/"></div>
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"upeng"};
		(function() {
			var ds = document.createElement('script');
			ds.type = 'text/javascript';ds.async = true;
			ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
			ds.charset = 'UTF-8';
			(document.getElementsByTagName('head')[0]
			 || document.getElementsByTagName('body')[0]).appendChild(ds);
		})();
	</script>
</div>

            
        </div>
        <div class="column one-fourth">
            
                
                


<h3>Post Directory</h3>

<div id="post-directory-module">
	<section class="post-directory">
		<p><strong class="toc-title">文章目录</strong></p>
		<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#blade模板引擎使用"><span class="toc-text">blade模板引擎使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#blade中的条件判断和循环操作"><span class="toc-text">blade中的条件判断和循环操作</span></a></li></ol>
	</section>
</div>
            
        </div>
    </div>
</section>

<footer class="container">
    <div class="site-footer" role="contentinfo">
        <div class="copyright left mobile-block">
                © 2016
                <span title="yupeng">yupeng</span>
                <a href="javascript:window.scrollTo(0,0)" class="right mobile-visible">TOP</a>
        </div>

        <ul class="site-footer-links right mobile-hidden">
            <li>
                <a href="javascript:window.scrollTo(0,0)" >TOP</a>
            </li>
        </ul>

        <a href="https://github.com/upeng" target="_blank" aria-label="view source code">
            <span class="mega-octicon octicon-mark-github" title="GitHub"></span>
        </a>

        <ul class="site-footer-links mobile-hidden">
            
                  
                  <li>
                    <a href="/"  title="Home">Home</a>
                  </li>
            
                  
                  <li>
                    <a href="/categories/"  title="Categories">Categories</a>
                  </li>
            
                  
                  <li>
                    <a href="/bookmark/"  title="Bookmark">Bookmark</a>
                  </li>
            
                  
                  <li>
                    <a href="http://shareup.sinaapp.com"  title="Share">Share</a>
                  </li>
            
                  
                  <li>
                    <a href="/about/"  title="About">About</a>
                  </li>
            
            <li>
                <a href="/atom.xml">
                    <span class="octicon octicon-rss" style="color:orange;"></span>
                </a>
            </li>
        </ul>
    </div>
</footer>


		<script src="/js/geopattern.js"></script>

		
			<script src="/js/toc.js"></script>
		

		<script src="/js/highlight.pack.js"></script>
		<script src="/lib/fancybox/jquery.fancybox-1.3.4.pack.js"></script>

		<script src="/js/index.js"></script>

		 <script src="/js/popular_repo.js"></script> 

	</body>
</html>